package com.ruoyi.flowable.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.flowable.domain.bo.AppBo;
import com.ruoyi.flowable.domain.bo.FormBo;
import com.ruoyi.flowable.domain.vo.AppVo;
import com.ruoyi.flowable.domain.vo.FormVersionNameVo;
import com.ruoyi.flowable.domain.vo.FormVo;
import com.ruoyi.flowable.service.IAppService;
import com.ruoyi.flowable.service.IFormService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 事项
 *
 * @author zxw
 * @date 2023-03-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/flowable/app")
public class AppController extends BaseController {

    private final IAppService iAppService;
    private final IFormService formService;
    /**
     * 查询事项列表
     */
    @SaCheckPermission("flowable:app:list")
    @GetMapping("/list")
    public TableDataInfo<AppVo> list(AppBo bo, PageQuery pageQuery) {
        return iAppService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出事项列表
     */
    @SaCheckPermission("flowable:app:export")
    @Log(title = "事项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppBo bo, HttpServletResponse response) {
        List<AppVo> list = iAppService.queryList(bo);
        ExcelUtil.exportExcel(list, "事项", AppVo.class, response);
    }

    /**
     * 获取事项详细信息
     *
     * @param appId 主键
     */
    @SaCheckPermission("flowable:app:query")
    @GetMapping("/{appId}")
    public R<AppVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long appId) {
        return R.ok(iAppService.queryById(appId));
    }

    /**
     * 新增事项
     */
    @SaCheckPermission("flowable:app:add")
    @Log(title = "事项", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppBo bo) {
        return toAjax(iAppService.insertByBo(bo));
    }

    /**
     * 修改事项
     */
    @SaCheckPermission("flowable:app:edit")
    @Log(title = "事项", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppBo bo) {
        return toAjax(iAppService.updateByBo(bo));
    }

    /**
     * 删除事项
     *
     * @param appIds 主键串
     */
    @SaCheckPermission("flowable:app:remove")
    @Log(title = "事项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{appIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] appIds) {
        return toAjax(iAppService.deleteWithValidByIds(Arrays.asList(appIds), true));
    }

    /**
     * 发布或者停用事项
     */
    @SaCheckPermission("flowable:app:updateStatus")
    @Log(title = "事项详情", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/updateStatus/{appId}")
    public R<Void> updateStatus(@PathVariable("appId") Long appId)
    {
        return toAjax(iAppService.updateStatus(appId));
    }

    /**
     * 表单列表
     */

    @GetMapping(value = "/getFormList")
    public R<List<FormVo>> getFormList()
    {
        FormBo formBo  = new FormBo();
        return R.ok(formService.queryList(formBo));
    }


    @GetMapping(value = "/getFormVersionList/{formId}")
    public R<List<FormVersionNameVo>> getFormVersionList(@PathVariable("formId") Long formId)
    {
        List<FormVersionNameVo> list = formService.selectFormVersionList(formId);
        return R.ok(list);
    }


}
